<?php
if(!defined('IN_DISCUZ')){
     exit('Access Denied');
     }

class plugin_video_autopic{
     }
class plugin_video_autopic_forum extends plugin_video_autopic{
	
     function post_video_autopic(){
         global $_G;
         loadcache('plugin');
         $var = $_G['cache']['plugin']['video_autopic'];
         $open = $var['zongkaiguan'];
         $fids = unserialize($var['bankuaiid']);
        
         if($open == 1 && in_array($_GET['fid'], $fids)){
             if(preg_match_all("/\[media[^\]]*\](.*)\[\/media\]/isU", $_GET['message'], $result)){
                 $flash = $result[1][0];
				
                 $flash_str = $this -> parseflv_big($flash);
                 if($flash_str['imgurl']){
                     $src = $flash_str['imgurl'];
                     $ym = date('Ym');
                     $d = date('d');
                     $his = date('His');
                     require_once libfile('class/image');
                     $romoteimage = new image;
                     $this -> checkattachdir($ym, $d);
                     $localattachment = $ym . "/" . $d . "/" . $his . strtolower(random(16)) . ".jpg";
                     $localurl = $_G['setting']['attachdir'] . "/forum/" . $localattachment;
                     $attachsaved = $this -> downloadpic($src, $localurl);
                     if($attachsaved){
                         $watermarkstatus = unserialize($_G['setting']['watermarkstatus']);
                         if($watermarkstatus['forum'] && empty($_G['forum']['disablewatermark'])){
                             $romoteimage -> Watermark($localurl);
                             }
                         $width = $attachsaved;
                         $path_parts = pathinfo($src);
                         $picname = $path_parts['filename'];
                         $filename = $picname . '.jpg';
                         $filesize = filesize($localurl);
                         $isimage = 1;
                         $remote = 0;
                         if(!$remote){
                             $thumb = $romoteimage -> Thumb($localurl, '', $_G['setting']['thumbwidth'], $_G['setting']['thumbheight'], $_G['setting']['thumbstatus'], $_G['setting']['thumbsource']) ? 1 : 0;
                             if(!$_G['setting']['thumbsource']) $width = $romoteimage -> imginfo['width'];
                             }
                        
                         $aid = C :: t('forum_attachment') -> insert(array('aid' => NULL, 'tid' => '0', 'pid' => '0', 'uid' => $_G['uid'], 'tableid' => '127', 'downloads' => '0'), true);
                         C :: t('forum_attachment_unused') -> insert(array('aid' => $aid, 'uid' => $_G['uid'], 'dateline' => $_G['timestamp'], 'filename' => $filename, 'filesize' => $filesize, 'attachment' => $localattachment, 'remote' => $remote, 'isimage' => $isimage, 'width' => $width, 'thumb' => $thumb));
                         $_GET['attachnew'][$aid] = array();
                         $strfirst = strpos($_GET['message'], $result[0][$key]);
                        
                         }
                     }
                 }
             }
        
         }
    
     function checkattachdir($ym, $d){
         global $_G;
         if(!is_dir($_G['setting']['attachdir'] . "/forum/" . $ym . "/")) mkdir($_G['setting']['attachdir'] . "/forum/" . $ym . "/");
         if(!is_dir($_G['setting']['attachdir'] . "/forum/" . $ym . "/" . $d . "/")) mkdir($_G['setting']['attachdir'] . "/forum/" . $ym . "/" . $d . "/");
         }
    
     function downloadpic($src, $localurl){
         if(file_exists($localurl) || copy($src, $localurl)){
             }elseif ($content = file_get_contents($src)){
             $fp = fopen($localurl, "w");
             fwrite($fp, $content);
             fclose($fp);
             }
         $size = getimagesize($localurl);
         if(!$size[0] || !$size[2]){
             unlink($localurl);
             return false;
             }else return $size[0];
         return false;
         }

     function parseflv_big($url, $width = 0, $height = 0){
         $lowerurl = strtolower($url);
         $flv = $iframe = $imgurl = '';
         if($lowerurl != str_replace(array('player.youku.com/player.php/sid/', 'tudou.com/v/', 'player.ku6.com/refer/'), '', $lowerurl)){
             $flv = $url;
             }elseif(strpos($lowerurl, 'v.youku.com/v_show/') !== FALSE){
             $ctx = stream_context_create(array('http' => array('timeout' => 10)));
             if(preg_match("/http:\/\/v.youku.com\/v_show\/id_([^\/]+)(.html|)/i", $url, $matches)){
                 $flv = 'http://player.youku.com/player.php/sid/' . $matches[1] . '/v.swf';
                 $iframe = 'http://player.youku.com/embed/' . $matches[1];
                 if(!$width && !$height){
                     $api = 'http://v.youku.com/player/getPlayList/VideoIDS/' . $matches[1];
                     $str = stripslashes(file_get_contents($api, false, $ctx));
                     if(!empty($str) && preg_match("/\"logo\":\"(.+?)\"/i", $str, $image)){
                         $url = substr($image[1], 0, strrpos($image[1], '/') + 1);
                         $filename = substr($image[1], strrpos($image[1], '/') + 2);
                         $imgurl = $url . '1' . $filename;
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'tudou.com/programs/view/') !== FALSE){
             if(preg_match("/http:\/\/(www.)?tudou.com\/programs\/view\/([^\/]+)/i", $url, $matches)){
                 $flv = 'http://www.tudou.com/v/' . $matches[2];
                 $iframe = 'http://www.tudou.com/programs/view/html5embed.action?code=' . $matches[2];
                 if(!$width && !$height){
                     $str = file_get_contents($url, false, $ctx);
                     if(!empty($str) && preg_match("/<span class=\"s_pic\">(.+?)<\/span>/i", $str, $image)){
                         $imgurl = trim($image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'v.ku6.com/show/') !== FALSE){
             if(preg_match("/http:\/\/v.ku6.com\/show\/([^\/]+).html/i", $url, $matches)){
                 $flv = 'http://player.ku6.com/refer/' . $matches[1] . '/v.swf';
                 if(!$width && !$height){
                     $api = 'http://vo.ku6.com/fetchVideo4Player/1/' . $matches[1] . '.html';
                     $str = file_get_contents($api, false, $ctx);
                     if(!empty($str) && preg_match("/\"bigpicpath\":\"(.+?)\"/i", $str, $image)){
                         $imgurl = str_replace(array('\u003a', '\u002e'), array(':', '.'), $image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'v.ku6.com/special/show_') !== FALSE){
             if(preg_match("/http:\/\/v.ku6.com\/special\/show_\d+\/([^\/]+).html/i", $url, $matches)){
                 $flv = 'http://player.ku6.com/refer/' . $matches[1] . '/v.swf';
                 if(!$width && !$height){
                     $api = 'http://vo.ku6.com/fetchVideo4Player/1/' . $matches[1] . '.html';
                     $str = file_get_contents($api, false, $ctx);
                     if(!empty($str) && preg_match("/\"bigpicpath\":\"(.+?)\"/i", $str, $image)){
                         $imgurl = str_replace(array('\u003a', '\u002e'), array(':', '.'), $image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'v.ku6.com/film/show_') !== FALSE){
             if(preg_match("/http:\/\/v.ku6.com\/film\/show_\d+\/([^\/]+).html/i", $url, $matches)){
                 $flv = 'http://player.ku6.com/refer/' . $matches[1] . '/v.swf';
                 if(!$width && !$height){
                     $api = 'http://vo.ku6.com/fetchVideo4Player/1/' . $matches[1] . '.html';
                     $str = file_get_contents($api, false, $ctx);
                     if(!empty($str) && preg_match("/\"bigpicpath\":\"(.+?)\"/i", $str, $image)){
                         $imgurl = str_replace(array('\u003a', '\u002e'), array(':', '.'), $image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'www.youtube.com/watch?') !== FALSE){
             if(preg_match("/http:\/\/www.youtube.com\/watch\?v=([^\/&]+)&?/i", $url, $matches)){
                 $flv = 'http://www.youtube.com/v/' . $matches[1] . '&hl=zh_CN&fs=1';
                 $iframe = 'http://www.youtube.com/embed/' . $matches[1];
                 if(!$width && !$height){
                     $str = file_get_contents($url, false, $ctx);
                     if(!empty($str) && preg_match("/'VIDEO_HQ_THUMB':\s'(.+?)'/i", $str, $image)){
                         $url = substr($image[1], 0, strrpos($image[1], '/') + 1);
                         $filename = substr($image[1], strrpos($image[1], '/') + 3);
                         $imgurl = $url . $filename;
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'tv.mofile.com/') !== FALSE){
             if(preg_match("/http:\/\/tv.mofile.com\/([^\/]+)/i", $url, $matches)){
                 $flv = 'http://tv.mofile.com/cn/xplayer.swf?v=' . $matches[1];
                 if(!$width && !$height){
                     $str = file_get_contents($url, false, $ctx);
                     if(!empty($str) && preg_match("/thumbpath=\"(.+?)\";/i", $str, $image)){
                         $imgurl = trim($image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'v.mofile.com/show/') !== FALSE){
             if(preg_match("/http:\/\/v.mofile.com\/show\/([^\/]+).shtml/i", $url, $matches)){
                 $flv = 'http://tv.mofile.com/cn/xplayer.swf?v=' . $matches[1];
                 if(!$width && !$height){
                     $str = file_get_contents($url, false, $ctx);
                     if(!empty($str) && preg_match("/thumbpath=\"(.+?)\";/i", $str, $image)){
                         $imgurl = trim($image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'video.sina.com.cn/v/b/') !== FALSE){
             if(preg_match("/http:\/\/video.sina.com.cn\/v\/b\/(\d+)-(\d+).html/i", $url, $matches)){
                 $flv = 'http://vhead.blog.sina.com.cn/player/outer_player.swf?vid=' . $matches[1];
                 if(!$width && !$height){
                     $api = 'http://interface.video.sina.com.cn/interface/common/getVideoImage.php?vid=' . $matches[1];
                     $str = file_get_contents($api, false, $ctx);
                     if(!empty($str)){
                         $imgurl = str_replace('imgurl=', '', trim($str));
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'you.video.sina.com.cn/b/') !== FALSE){
             if(preg_match("/http:\/\/you.video.sina.com.cn\/b\/(\d+)-(\d+).html/i", $url, $matches)){
                 $flv = 'http://vhead.blog.sina.com.cn/player/outer_player.swf?vid=' . $matches[1];
                 if(!$width && !$height){
                     $api = 'http://interface.video.sina.com.cn/interface/common/getVideoImage.php?vid=' . $matches[1];
                     $str = file_get_contents($api, false, $ctx);
                     if(!empty($str)){
                         $imgurl = str_replace('imgurl=', '', trim($str));
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'http://my.tv.sohu.com/u/') !== FALSE){
             if(preg_match("/http:\/\/my.tv.sohu.com\/u\/[^\/]+\/(\d+)/i", $url, $matches)){
                 $flv = 'http://v.blog.sohu.com/fo/v4/' . $matches[1];
                 if(!$width && !$height){
                     $api = 'http://v.blog.sohu.com/videinfo.jhtml?m=view&id=' . $matches[1] . '&outType=3';
                     $str = file_get_contents($api, false, $ctx);
                     if(!empty($str) && preg_match("/\"cutCoverURL\":\"(.+?)\"/i", $str, $image)){
                         $imgurl = str_replace(array('\u003a', '\u002e'), array(':', '.'), $image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'http://v.blog.sohu.com/u/') !== FALSE){
             if(preg_match("/http:\/\/v.blog.sohu.com\/u\/[^\/]+\/(\d+)/i", $url, $matches)){
                 $flv = 'http://v.blog.sohu.com/fo/v4/' . $matches[1];
                 if(!$width && !$height){
                     $api = 'http://v.blog.sohu.com/videinfo.jhtml?m=view&id=' . $matches[1] . '&outType=3';
                     $str = file_get_contents($api, false, $ctx);
                     if(!empty($str) && preg_match("/\"cutCoverURL\":\"(.+?)\"/i", $str, $image)){
                         $imgurl = str_replace(array('\u003a', '\u002e'), array(':', '.'), $image[1]);
                         }
                     }
                 }
             }elseif(strpos($lowerurl, 'http://www.ouou.com/fun_funview') !== FALSE){
             $str = file_get_contents($url, false, $ctx);
             if(!empty($str) && preg_match("/var\sflv\s=\s'(.+?)';/i", $str, $matches)){
                 $flv = $_G['style']['imgdir'] . '/flvplayer.swf?&autostart=true&file=' . urlencode($matches[1]);
                 if(!$width && !$height && preg_match("/var\simga=\s'(.+?)';/i", $str, $image)){
                     $imgurl = trim($image[1]);
                     }
                 }
             }elseif(strpos($lowerurl, 'http://www.56.com') !== FALSE){
            
             if(preg_match("/http:\/\/www.56.com\/\S+\/play_album-aid-(\d+)_vid-(.+?).html/i", $url, $matches)){
                 $flv = 'http://player.56.com/v_' . $matches[2] . '.swf';
                 $matches[1] = $matches[2];
                 }elseif(preg_match("/http:\/\/www.56.com\/\S+\/([^\/]+).html/i", $url, $matches)){
                 $flv = 'http://player.56.com/' . $matches[1] . '.swf';
                 }
             if(!$width && !$height && !empty($matches[1])){
                 $api = 'http://vxml.56.com/json/' . str_replace('v_', '', $matches[1]) . '/?src=out';
                 $str = file_get_contents($api, false, $ctx);
                 if(!empty($str) && preg_match("/\"bimg\":\"(.+?)\"/i", $str, $image)){
                     $imgurl = trim($image[1]);
                     }
                 }
             }
         if($flv){
               return array('flv' => $flv, 'imgurl' => $imgurl);
             }else{
             return FALSE;
             }
        }
    }

?>